package com.drason.encapsulation.loadmore;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.drason.encapsulation.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by drason on 2017/11/6.
 */

public class TestMyloadMoreActivity extends AppCompatActivity {

    @BindView(R.id.rv_testmy)
    MyLoadMoreRecyclerView mRecyclerView;

    private MyTestAdapter mAdapter;

    private List<String> dataList = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent(context,TestMyloadMoreActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_myloadmore);
        ButterKnife.bind(this);
        getData();
        initRecyclerview();
    }

    private void initRecyclerview() {
        mAdapter = new MyTestAdapter();
        GridLayoutManager manager = new GridLayoutManager(this,2);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setEndLayoutRes(R.layout.test2_end);
        mAdapter.addItems(dataList);
        mRecyclerView.notifyData();
        mRecyclerView.setHasMore(true);

        mRecyclerView.setOnLoadMoreListener(new MyLoadMoreRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMoreListener() {
                final List<String> list = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    list.add("add item: " + i);
                }

                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new TimerTask() {
                            @Override
                            public void run() {
                                mAdapter.addItems(list);
                                mRecyclerView.notifyData();
                                mRecyclerView.setHasMore(false);
                            }
                        });
                    }
                },1000);
            }
        });
    }


    private void getData() {
        char letter = 'A';
        for (int i = 0; i < 26; i++) {
            dataList.add(String.valueOf(letter));
            letter++;
        }
    }


    public class MyTestAdapter extends RecyclerView.Adapter<MyTestVH> {

        private List<String> mDataSet;

        public MyTestAdapter() {
            mDataSet = new ArrayList<>();
        }

        public void addItems(List<String> dataSet) {
            mDataSet.removeAll(dataSet);
            mDataSet.addAll(dataSet);
        }

        @Override
        public MyTestVH onCreateViewHolder(ViewGroup parent, int viewType) {
            return MyTestVH.instance(parent.getContext());
        }

        @Override
        public void onBindViewHolder(MyTestVH holder, int position) {
            holder.bindData(mDataSet.get(position));
        }

        @Override
        public int getItemCount() {
            return mDataSet.size();
        }
    }

    static class MyTestVH extends RecyclerView.ViewHolder {

        public static MyTestVH instance(Context context) {
            View view = LayoutInflater.from(context).inflate(R.layout.vh_mytest, null);
            return new MyTestVH(view);
        }

        @BindView(R.id.tv_mytest)
        TextView mTvMyTest;

        public MyTestVH(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }

        public void bindData(String str) {
            mTvMyTest.setText(str);
        }
    }
}
